﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DanhSachThiDAL
/// </summary>
public class DanhSachThiDAL
{

		public static object IsNull(object checkValue, object replaceValue)
    {
        if (checkValue.Equals(System.DBNull.Value) || checkValue == null)
            return replaceValue;
        return checkValue;
    }

    public static List<DanhSachThiINFO> listAll()
    {
        string sql = "select * from DSTHI";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetListObjects<DanhSachThiINFO>(dr);
    }

    public static DanhSachThiINFO GetInfo(int mahocvien)
    {
        string sql = "select * from DSTHI where MaHV ="+mahocvien+"";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        return SQLHelper.SetObject<DanhSachThiINFO>(dr);
    }

    public static int Insert(DanhSachThiINFO obj)
    {
        obj.MaDuThi = GetMa();
        obj.MaDuThi = obj.MaDuThi + 1;
        string sql = "INSERT INTO DSTHI (MaHV,TenHV,MaDuThi,NgaySinh,GioiTinh) VALUES (" + obj.MaHV + ",N'" + obj.TenHV + "'," + obj.MaDuThi + "," + obj.NgaySinh + ",'" + obj.GioiTinh + "')";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Update(DanhSachThiINFO obj)
    {
        string sql = "Update DSTHI set TenHV='" + obj.TenHV + "' Where mahocvien=" + obj.MaHV + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int Delete(int mahocvien)
    {
        string sql = "Delete from DSTHI where mahocvien =" + mahocvien + "";
        return SQLHelper.ExecuteNonQuery(Conn.ConnectionString, CommandType.Text, sql, null);
    }

    public static int GetMa()
    {
        string sql = "SELECT ISNULL(MAX(MaHV),0) FROM DSTHI";
        SqlDataReader dr = SQLHelper.ExecuteReader(Conn.ConnectionString, CommandType.Text, sql, null);
        int ma = 0;
        if (dr.Read()) {
            ma = int.Parse(dr[0].ToString());
        }
        dr.Close();
        return ma;
    }
	
}